﻿#pragma once
//#include "wx/wxprec.h"
//#include	<wx/log.h>

struct readerInterface
{
	bool (*openCom)(int portnum);
	void (*closeCom)();

	bool (*pgetx)(char* pdata) ;
	bool (*pgety)(char* pdata) ;
	bool (*pgetall)(char* pdata) ;
};
extern serialPort com;


inline bool openCom(int portnum)
{
	if( com.OpenCom(portnum))
	{
		return true;
	}
	return false;
}

inline void closeCom()
{
	com.CloseCom();
}

inline bool send(const char* cmd)
{
	return com.WriteCom(cmd);
}
inline bool read(char* dat)
{
	return com.ReadCom(dat);
}

inline void waitcom()
{
	OVERLAPPED ov;
	memset(&ov,0,sizeof(OVERLAPPED));
	DWORD dwEventMask;
	while(true)
	{
		ov.hEvent=CreateEventA(NULL,TRUE,FALSE,NULL);
		if (WaitCommEvent(com.hComm, &dwEventMask, &ov)) 
		{
			MessageBoxW(0,L"立即有数据？",L"锁相串口",0);
			if (dwEventMask & EV_RXCHAR) 
			{
				MessageBoxW(0,L"确实如此",L"锁相串口",0);
			}
		}
		else
		{
			DWORD dwRet = GetLastError();
			if( ERROR_IO_PENDING == dwRet)
			{
				WaitForSingleObject(ov.hEvent,INFINITE);
				DWORD dwerror;
				COMSTAT stat;
				ClearCommError(com.hComm,&dwerror, &stat);
				if(stat.cbInQue==0)
					continue;
				else
					break;
			}
			else 
			{
				char ssss[200];
				sprintf(ssss,"等待错误 code=%d",dwRet);
				MessageBoxA(0,ssss,"锁相串口",0);
			}
		}
		CloseHandle(ov.hEvent);//ClearCommError(com.hComm,NULL,NULL);
	}
}
